package view 
{
	import adobe.utils.CustomActions;
	import controller.ControllerManager;
	import fl.transitions.easing.Strong;
	import fl.transitions.Tween;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import model.Post;	
	import view.graph.GraphManager;
	import view.ressources.GraphicRessources;
	import view.utils.Tools;
	/**
	 * ...
	 * @author Anthony
	 */
	public class GraphicPost 
	{
		//------------------------------- statics ------------------------------\\
		private static const GRAPHICPOSTWIDTH 	: int = 300;
		private static const MAXMESSAGEHEIGHT 	: int = 150;
		private static const HEADHEIGHT			: int = 20;
		private static const MESSAGEMARGIN		: int = 5;
		private static const HEADTEXTFORMAT		: TextFormat = new TextFormat(null, 14, null, null, null, null, null, null, null, 15);
		private static const MESSAGETEXTFORMAT	: TextFormat = new TextFormat(null, 14, null, null, null, null, null, null, null, MESSAGEMARGIN, MESSAGEMARGIN);
		
		//------------------------------ variables -----------------------------\\
		protected var _graphic 				: Sprite;
		protected var _guiContainer			: Sprite;
		protected var _postContainer		: Sprite;
		protected var _backgroundContainer	: Sprite;
		
		protected var _head 				: TextField;
		protected var _message 				: TextField;
		protected var _isSelectedBackground	: Bitmap;
			
		protected var _data 				: Post;
		protected var _graphicAnswerOf		: GraphicPost;
		protected var _graphicAnswers		: Array /*GraphicPost*/;
			
		protected var _graphSonsLength		: int;
		
		//------------------------------ accessors -----------------------------\\
		public function get graphic() 								: Sprite 				{ return _graphic; }
		public function get guiContainer()							: Sprite 				{ return _guiContainer; }
		public function get data() 									: Post 					{ return _data; }
		public function get graphicAnswerOf() 						: GraphicPost			{ return _graphicAnswerOf; } 
		public function set graphicAnswerOf(father : GraphicPost) 	: void					{ _graphicAnswerOf = father; } 
		public function get graphicAnswers() 						: Array /*GraphicPost*/	{ return _graphicAnswers; } 
		public function get graphSonsLength() 						: int 					{ return _graphSonsLength; }
		public function set graphSonsLength(val : int) 				: void					{ _graphSonsLength = val; }		
		
		//-------------------------- public functions --------------------------\\
		public function GraphicPost(data : Post) 
		{
			_data = data;
			
			_graphicAnswers 		= new Array();
			
			_graphic 				= new Sprite();
			_guiContainer 			= new Sprite();
			_postContainer			= new Sprite();
			_backgroundContainer	= new Sprite();
			
			_graphic.addChild(_backgroundContainer);
			_graphic.addChild(_postContainer);
			_graphic.addChild(_guiContainer);
						
			_head = new TextField();
			_message = new TextField();
			
			_postContainer.addChild(_head);
			_postContainer.addChild(_message);
		}
		
		public function init() : void 
		{
			var graphicPostBackgroundMargin : Number = (GraphicRessources.self.graphicPostHeadBackground_SELECTED.width - GraphicRessources.self.graphicPostHeadBackground.width);
			
			_head.x = _head.y = (graphicPostBackgroundMargin / 2);
			_head.width = GRAPHICPOSTWIDTH;
			_head.height = HEADHEIGHT;
			_head.text = data.key;
			_head.setTextFormat(HEADTEXTFORMAT);
			
			_message.x = (graphicPostBackgroundMargin / 2);
			_message.y = _head.y + _head.height + MESSAGEMARGIN;
			_message.multiline = true;
			_message.wordWrap = true;
			_message.text = data.message;
			_message.setTextFormat(MESSAGETEXTFORMAT);
			_message.width = GRAPHICPOSTWIDTH;
			_message.height = Math.max(HEADHEIGHT, Math.min(MAXMESSAGEHEIGHT, _message.textHeight + MESSAGEMARGIN));
						
			var background : Bitmap = Tools.getRepeatedBackgroundFromHeadCorpsFoot(	GraphicRessources.self.graphicPostHeadBackground, 
																					GraphicRessources.self.graphicPostCorpsBackground, 
																					GraphicRessources.self.graphicPostFootBackground, 
																					(_message.y + _message.height));
			background.x = background.y = (graphicPostBackgroundMargin / 2);
			_backgroundContainer.addChild(background);			
			
			_isSelectedBackground = Tools.getRepeatedBackgroundFromHeadCorpsFoot(	GraphicRessources.self.graphicPostHeadBackground_SELECTED,
																					GraphicRessources.self.graphicPostCorpsBackground_SELECTED,
																					GraphicRessources.self.graphicPostFootBackground_SELECTED,
																					(_message.y + _message.height + graphicPostBackgroundMargin));
			_isSelectedBackground.visible = false;
			_backgroundContainer.addChild(_isSelectedBackground);
		}
		
		public function addAnswer(post : GraphicPost) : void
		{
			_graphicAnswers.push(post);
			post._graphicAnswerOf = this;
		}
		
		public function getAnswersWidthSum(graphDepth : int = -1) : Number
		{
			if ((graphDepth == 0) || (_graphicAnswers.length == 0))
				return _graphic.width;
			else
			{
				var totalWidth : int = 0;
				for (var graphicAnswerKey : String in _graphicAnswers)
					totalWidth += _graphicAnswers[graphicAnswerKey].getAnswersWidthSum(graphDepth - 1);
				return totalWidth;
			}
		}
		
		public function changeAnswersVisibility(visible : Boolean, graphDepth : int = -1) : void
		{
			_graphic.visible = visible;
						
			if ((graphDepth != 0) && (_graphicAnswers.length > 0))
			{
				for (var graphicAnswerKey : String in _graphicAnswers)
					_graphicAnswers[graphicAnswerKey].changeAnswersVisibility(visible, graphDepth - 1);
			}
		}
		
		public function select() : void
		{
			_isSelectedBackground.visible = true;
		}
		
		public function unselect() : void
		{
			_isSelectedBackground.visible = false;
		}
		
		//------------------------- private functions --------------------------\\
		
	}

}